<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitealbum
 * @copyright  Copyright 2010-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: Controller.php 2011-08-026 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitealbum_Widget_ProfilePhotosController extends Engine_Content_Widget_Abstract {

  protected $_childCount;

  public function indexAction() {
    // Don't render this if not authorized
    $viewer = Engine_Api::_()->user()->getViewer();
    if (!Engine_Api::_()->core()->hasSubject()) {
      return $this->setNoRender();
    }

    // Get subject and check auth
    $subject = Engine_Api::_()->core()->getSubject();
    if (!$subject->authorization()->isAllowed($viewer, 'view')) {
      return $this->setNoRender();
    }

    // Just remove the title decorator
    $this->getElement()->removeDecorator('Title');

    // Get paginator
    if (!Engine_Api::_()->sitealbum()->isLessThan417AlbumModule()) {
      $this->view->paginator = $paginator = Engine_Api::_()->getItemTable('album')
                      ->getAlbumPaginator(array('owner' => $subject, 'search' => 1));
    } else {
      $this->view->paginator = $paginator = Engine_Api::_()->getApi('core', 'album')
                      ->getAlbumPaginator(array('owner' => $subject, 'search' => 1));
    }

    // Set item count per page and current page number
    $paginator->setItemCountPerPage($this->_getParam('itemCountPerPage', 8));
    $paginator->setCurrentPageNumber($this->_getParam('page', 1));

    $this->view->taggedInOthersPhotos = $taggedInOthersPhotos = Engine_Api::_()->sitealbum()->getTaggedInOthersPhotos($subject->getIdentity());
    $this->taggedInOthersPhotosCount = $taggedInOthersPhotosCount = count($taggedInOthersPhotos);
    // Do not render if nothing to show
    if ($paginator->getTotalItemCount() <= 0 && $taggedInOthersPhotosCount <= 0) {
      return $this->setNoRender();
    }
    $this->view->showLightBox = Engine_Api::_()->sitealbum()->showLightBoxPhoto();
    if ($this->view->showLightBox) {
      $this->view->params = $params = array('type' => 'tagged', 'count' => $taggedInOthersPhotosCount, 'owner_id' => $subject->getIdentity());
    }
    // Add count to title if configured
    if ($this->_getParam('titleCount', false) && $paginator->getTotalItemCount() > 0) {
      $this->_childCount = $paginator->getTotalItemCount();
    }
  }

  public function getChildCount() {
    return $this->_childCount;
  }

}